package com.howie.parnote.web.view;

import com.howie.parnote.base.EnumModelAndView;
import com.howie.parnote.constant.RespMessageConstant;
import com.howie.parnote.constant.ViewConstant;
import com.howie.parnote.module.IncidentModule;
import com.howie.parnote.module.LocationModule;
import com.howie.parnote.module.RoleModule;
import com.howie.parnote.module.interaction.NoteTag;
import com.howie.parnote.util.JSONUtil;
import com.howie.parnote.util.StringUtil;
import com.howie.parnote.util.security.DESCoder;
import com.howie.parnote.web.ParnoteBaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class MainViewController extends ParnoteBaseController {

	@RequestMapping(value = "/main/tags/{tagType}", method = RequestMethod.GET)
	public
	@ResponseBody
	String getNoteTagJson(@PathVariable() String tagType) {
		List<NoteTag> tags = null;
		try {
			if (tagType.equals("role")) {
				tags = getRoleNoteTags();
			} else if (tagType.equals("location")) {
				tags = getLocationNoteTags();
			} else if (tagType.equals("incident")) {
				tags = getIncidentNoteTags();
			}
		} catch (Exception e) {
			logger.error("TAG json请求错误", e);
			return failResponse(RespMessageConstant.SYSTEM_ERROR);
		}
		return JSONUtil.getJsonArray(tags);
	}

	private List<NoteTag> getIncidentNoteTags() throws Exception {
		List<IncidentModule> incidents = incidentService.selectAll();
		List<NoteTag> tags = new ArrayList<NoteTag>();
		for (IncidentModule incident : incidents) {
			NoteTag noteTag = new NoteTag();
			noteTag.setTagIds(DESCoder.getHexEncodeId(incident.getIncidentId()));
			noteTag.setTagName(incident.getIncidentText());
			tags.add(noteTag);
		}
		return tags;
	}

	private List<NoteTag> getLocationNoteTags() throws Exception {
		List<NoteTag> tags = new ArrayList<NoteTag>();
		List<LocationModule> locations = locationService.selectAll();
		for (LocationModule location : locations) {
			NoteTag noteTag = new NoteTag();
			noteTag.setTagIds(DESCoder.getHexEncodeId(location.getLocationId()));
			noteTag.setTagName(location.getLocationText());
			tags.add(noteTag);
		}
		return tags;
	}

	private List<NoteTag> getRoleNoteTags() throws Exception {
		List<RoleModule> roles = roleService.selectAll();
		List<NoteTag> tags = new ArrayList<NoteTag>();
		for (RoleModule role : roles) {
			NoteTag noteTag = new NoteTag();
			noteTag.setTagIds(DESCoder.getHexEncodeId(role.getRoleId()));
			noteTag.setTagName(role.getRoleText());
			tags.add(noteTag);
		}
		return tags;
	}


	@RequestMapping(value = "/explore")
	public ModelAndView explore(@RequestParam(value = "role", required = false) String roleCode,
								@RequestParam(value = "location", required = false) String locationCode,
								@RequestParam(value = "incident", required = false) String incidentCode) {
		ModelAndView mav = new EnumModelAndView(ViewConstant.VIEW_EXPLORE);
		tagListResponse(roleCode, locationCode, incidentCode, mav);
		return mav;
	}

	@RequestMapping(value = "/share")
	public ModelAndView share(@RequestParam(value = "role", required = false) String roleCode,
							  @RequestParam(value = "location", required = false) String locationCode,
							  @RequestParam(value = "incident", required = false) String incidentCode) {
		ModelAndView mav = new EnumModelAndView(ViewConstant.VIEW_SHARE);
		tagListResponse(roleCode, locationCode, incidentCode, mav);
		return mav;
	}

	/**
	 * 根据3个tag的请求返回相应list数据
	 *
	 * @param roleCode
	 * @param locationCode
	 * @param incidentCode
	 * @param mav
	 */
	private void tagListResponse(String roleCode, String locationCode, String incidentCode, ModelAndView mav) {
		if (viewRequest(roleCode, locationCode, incidentCode)) {
			List<RoleModule> roles = roleService.selectAll();
			mav.addObject("roles", roles);
		} else if (locationRequest(roleCode, locationCode, incidentCode)) {
			mav.setViewName(ViewConstant.VIEW_LOCATION_LIST.getPath());
			List<LocationModule> locations = locationService.selectAll();
			mav.addObject("locations", locations);
		} else if (eventRequest(roleCode, locationCode, incidentCode)) {
			mav.setViewName(ViewConstant.VIEW_INCIDENT_LIST.getPath());
			List<IncidentModule> incidents = incidentService.selectAll();
			mav.addObject("incidents", incidents);
		}
	}

	private boolean viewRequest(String roleCode, String locationCode, String whatCode) {
		return StringUtil.isEmpty(roleCode) && StringUtil.isEmpty(locationCode) && StringUtil.isEmpty(whatCode);
	}

	private boolean eventRequest(String roleCode, String locationCode, String whatCode) {
		return StringUtil.notEmpty(roleCode) && StringUtil.notEmpty(locationCode) && StringUtil.isEmpty(whatCode);
	}

	private boolean locationRequest(String roleCode, String locationCode, String whatCode) {
		return StringUtil.notEmpty(roleCode) && StringUtil.isEmpty(locationCode) && StringUtil.isEmpty(whatCode);
	}


}
